<style scoped>
</style>

<template>

  <div>

    <Button
      type="primary"
      :loading="modalForm_loading"
      @click="saveForm('modalForm')"
    >保存</Button>
  </div>

</template>

<script>
import {
  saveUser,
  roleComboList,
  getAreasCascader,
  getMaintenanceCombo
} from "./../../../api/getData";
export default {
  data() {
    return {
      IsModalShow: false,
      modalForm: {},
      modalForm_loading: false,
      deptCasc: [],
      roleComboList: [{ id: 1, roleName: "管理员" }],
      MaintenanceComboList: [],
      mainNameShow: false
    };
  },
  watch: {
    modalShow(curVal, oldVal) {
      this.modalForm = Object.assign(this.parentForm);
      //console.log(this.modalForm.depArray)

      if (this.parentForm.depArray && curVal) {
        this.modalForm.depArray = this.parentForm.depArray
          .split(",")
          .map(function(item) {
            return parseInt(item);
          });
      }
      this.IsModalShow = curVal;
      this.roleChange();
      if (this.modalFormTitle == "添加用户") {
        this.modalForm.rbac_RoleId = 1;
      }
    }
  },
  created() {
    this.getRoleComboList();
    this.getDeptCascList();
  },
  mounted() {
    this.getMaintenanceComboList();
  },
  methods: {
    async getRoleComboList() {
      let res = await roleComboList();
      this.roleComboList = res.data;
      //console.log(this.roleComboList);
    },
    async getDeptCascList() {
      let res = await getAreasCascader({ id: 0 });
      this.deptCasc = res.data;
      //console.log(this.deptCasc);
    },
    async getMaintenanceComboList() {
      let res = await getMaintenanceCombo();
      this.MaintenanceComboList = res.data;
      console.log("MaintenanceComboList" + this.MaintenanceComboList);
    },
    cancel() {
      this.$emit("listenModalForm");
    },
    roleChange() {
      let _this = this;
      this.mainNameShow =
        this.roleComboList.find(val => val.id == _this.modalForm.rbac_RoleId)
          .roleName == "维保人员";
      if (!this.mainNameShow) this.modalForm.smk_MaintenanceId = 0;

      // this.modalForm.smk_MaintenanceId=0;
    },
    saveForm(name) {
      this.$refs[name].validate(async valid => {
        if (valid) {
          // this.modalForm_loading=true;
          const params = this.modalForm;
          let arr = this.modalForm.depArray;
          params.depArray = arr.join(",");
          params.departmentId = arr[arr.length - 1];
          try {
            let result = await saveUser(params);

            if (result.code == "0") {
              this.$Message.success("提交成功!");
              this.$emit("listenModalForm");
              this.$emit("refreshTableList");
            } else {
              this.$Message.error(result.msg);
            }
          } catch (err) {
            console.log(err);
            this.$Message.error("服务器异常，稍后再试");
          }
          this.modalForm_loading = false;
        } else {
          this.$Message.error("表单验证失败!");
        }
      });
    }
  }
};
</script>
